#include <QtGui>
#include "register.h"

Register::Register(QWidget *parent) : QWidget(parent)
{
	setupUi(this);

	QRegExp regExp("[1][0-9]{10}");
	editPhone->setValidator(new QRegExpValidator(regExp, this));
	timer = new QTimer();
	timer->setInterval(1000);

	manager = new QNetworkAccessManager(this);
	managerImage = new QNetworkAccessManager(this);
	reply = NULL;
	sslCfg.setPeerVerifyMode(QSslSocket::VerifyNone);
	sslCfg.setProtocol(QSsl::TlsV1);
	request.setSslConfiguration(sslCfg);
	requestImage.setSslConfiguration(sslCfg);

	setRegisterDefault();

	connect(commitRegister, SIGNAL(clicked()), this, SLOT(checkRegister()));
	connect(editPhone, SIGNAL(returnPressed()), commitPhone, SIGNAL(clicked()));
	connect(editShortMsg, SIGNAL(returnPressed()), this, SLOT(checkRegister()));
	connect(editGraphics, SIGNAL(returnPressed()), this, SLOT(checkRegister()));
	connect(timer, SIGNAL(timeout()), this, SLOT(lcdDecrease()));

	this->setFixedSize(frameSize());
}

void Register::on_editPhone_textChanged()
{
	commitPhone->setEnabled(editPhone->hasAcceptableInput());
}

void Register::on_editShortMsg_textChanged()
{
	if (!editGraphics->text().isEmpty())
	{
		commitRegister->setEnabled(true);
	}
}

void Register::on_editGraphics_textChanged()
{
	if (!editShortMsg->text().isEmpty())
	{
		commitRegister->setEnabled(true);
	}
}

void Register::on_reset_clicked()
{
	setRegisterDefault();
}

void Register::setRegisterDefault()
{
	commitPhone->setEnabled(false);
	commitRegister->setEnabled(false);
	editPhone->clear();
	editPhone->setEnabled(true);
	editShortMsg->clear();
	editShortMsg->setEnabled(true);
	editGraphics->clear();
	editGraphics->setEnabled(true);
	showGraphics->clear();
	timer->stop();
	showTime->display(0);
	editPhone->setFocus();
	commitPhone->setText(FROMSTR("获取验证码"));
	commitPhone->setEnabled(true);
}

void Register::on_commitPhone_clicked()
{
	sendShortMsgRequest();
	sendGraphicsRequest();
}

void Register::on_regetGraphics_clicked()
{
	sendGraphicsRequest();
}

void Register::lcdDecrease()
{
	if (time == 0)
	{
		commitPhone->setEnabled(true);
		timer->stop();
		return;
	}
	time--;
	showTime->display(time);
}

void Register::sendShortMsgRequest()
{
	showResult->setText(FROMSTR("获取中..."));
	editPhone->setEnabled(false);
	/* Get the code */

	double r = qrand()*1.0/RAND_MAX;
	QString temp;
	temp.setNum(r, 'f', 17);
	QString str = "https://cmpay.10086.cn/service/send_chk_no.xhtml?REG_MBL_NO="+editPhone->text()+"&SMS_CD=URM001&typ=Y&r="+temp;
	request.setUrl(str);
	reply = manager->get(request);
	connect(reply, SIGNAL(finished()), this, SLOT(checkPhone()));
	r = qrand()*1.0/RAND_MAX;
}

void Register::sendGraphicsRequest()
{
	double r = qrand()*1.0/RAND_MAX;
	QString temp;
	temp.setNum(r, 'f', 17);
	QString str = "https://cmpay.10086.cn/service/image.xhtml?" + temp;
	requestImage.setUrl(str);
	replyImage = managerImage->get(requestImage);
	connect(replyImage, SIGNAL(finished()), this, SLOT(setImage()));
}

void Register::setImage()
{
	QImage image;
	image.loadFromData(replyImage->readAll());
	QPixmap map;
	map = map.fromImage(image);

	showGraphics->setPixmap(map);
	showGraphics->show();
	showResult->setText("输入验证码");
	replyImage->deleteLater();
}

void Register::checkPhone()
{
	QVariant status = reply->attribute(QNetworkRequest::HttpStatusCodeAttribute);
	if (status.toInt() != 200)
	{
		showResult->setText("网络错误");
	}
	QByteArray result = reply->readAll();
	if ((unsigned int)result.at(27) == 0xffffffb4)
	{
		showResult->setText("已注册");
		editPhone->setEnabled(true);
		return;
	}
	time = 180;
	showTime->display(time);
	timer->start();
	commitPhone->setText(FROMSTR("重新获取"));
	commitPhone->setEnabled(false);
	reply->deleteLater();
}

void Register::checkRegister()
{
	QString shortMsg = editShortMsg->text();
	if (shortMsg.isEmpty())
	{
		editShortMsg->setFocus();
		return;
	}

	QString graphics = editGraphics->text();
	if (graphics.isEmpty())
	{
		editGraphics->setFocus();
		return;
	}

	QString phone = editPhone->text();
	showResult->setText(FROMSTR("注册中.."));
	editShortMsg->setEnabled(false);
	editGraphics->setEnabled(false);
	/* register */
	showResult->setText(FROMSTR("成功"));
	setRegisterDefault();
}

